<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@ taglib prefix="his_fns" uri="/WEB-INF/tlds/his_fns.tld" %>
<script type="text/javascript">
    $.fn.serializeObject = function()
    {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

    function addrows(){
        if ($('#purchasePrice').val() == null || $('#purchasePrice').val() == "" || typeof($('#purchasePrice').val()) == "undefined") {
            toastr.error("进货价不能为空!");
            return;
        }
        if ($('#batchNo').val() == null || $('#batchNo').val() == "" || typeof($('#batchNo').val()) == "undefined") {
            toastr.error("批号不能为空!");
            return;
        }
        if ($('#buyQuantity').val() == null || $('#buyQuantity').val() == "" || typeof($('#buyQuantity').val()) == "undefined") {
            toastr.error("采购数量不能为空!");
            return;
        }
        var idxIndex = $('#idxIndex').val();  //下标

        var applicationId = $('#applicationId').val();
        var expInfoId = $('#expInfoId').val();
        var expCode = $('#expCode').val();
        var expName = $('#expName').val();
        var packageNum = $('#packageNum').val();
        var packageSpec = $('#packageSpec').val();
        var packageUnit = $('#packageUnits').val();
        var packageUnitName = $("#packageUnits").find("option:selected").text();
        var minSpec = $('#minSpec').val();
        var minUnits = $('#minUnits').val();
        var supplierId = $('#supplierId').val();
        var supplierName = $("#supplierId").find("option:selected").text();
        var purchasePrice = $('#purchasePrice').val();
        var batchNo = $('#batchNo').val();
        var expireDate = $('#expireDate').val();
        var sterilizationMethod = $('#sterilizationMethod').val();
        var sterilizationBatchNo = $('#sterilizationBatchNo').val();
        var sterilizationDate = $('#sterilizationDate').val();
        var buyQuantity = $('#buyQuantity').val();
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(5).find('input').val(expInfoId);
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(6).find('input').val(expCode);
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(7).html(expName);
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(8).find('input').val(packageNum);
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(9).find('input').val(packageSpec);
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(10).find('input').val(packageUnit);
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(11).html(packageUnitName);
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(12).find('input').val(minSpec);
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(13).find('input').val(minUnits);
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(14).html(supplierName);
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(15).find('input').val(purchasePrice);
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(16).find('input').val(batchNo);
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(17).find('input').val(expireDate);

        $("#mySelects"+ idxIndex +" option[value='"+sterilizationMethod+"']").attr("selected", true);
        $('#mySelects'+ idxIndex).change();

        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(19).find('input').val(sterilizationBatchNo);
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(20).find('input').val(sterilizationDate);
        $("#buyPlanBody").find("tr:eq("+ idxIndex +")").children().eq(21).find('input').val(buyQuantity);
        closeRight();
        $('#getBuyExpPlanData').attr('disabled',true);
    }
</script>
<div class="content-wrap">
    <div class="wrapper" style="bottom: 50px;">
        <ul class="nav nav-tabs">
            <li>
                <a href="#"  onclick="loadDiv('rigthCenterDiv','${ctx}/csm/buyExpPlan/updateForm')">修改</a>
            </li>
        </ul>
        <section class="panel panel-default">
            <div class="panel-body">
                <div class="col-lg-12">
                <form:form id="inputForm" data-parsley-validate=""  modelAttribute="expBuyPlan" onsubmit="return formSaveLoad('rigthCenterDiv','inputForm','${ctx}/csm/expStock/save',
                   '${ctx}/csm/expStock/index');" method="post" class="form-horizontal">
                    <input hidden="hidden" path="idxIndex" id="idxIndex" value="${idxIndex}"/>
                    <form:hidden path="expProvideApplication.id" id="applicationId"/>
                    <form:hidden path="purchaseNumber"/>
                    <form:hidden path="expProvideApplication.storageIn"/>
                    <form:hidden path="expProvideApplication.storageOut"/>
                    <form:hidden path="expProvideApplication.infoId"/>
                    <form:hidden path="expProvideApplication.expInfoId" id="expInfoId"/>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">编码：</label>
                        <div class="col-sm-8">
                            <form:input path="expProvideApplication.expCode" id="expCode" htmlEscape="false" class="form-control"  maxlength="32" readonly="true"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">名称：</label>
                        <div class="col-sm-8">
                            <form:input path="expName" htmlEscape="false" id="expName" class="form-control"  maxlength="64" />
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-sm-4 control-label">包装数量：</label>
                        <div class="col-sm-8">
                            <form:input path="expProvideApplication.packageNum" id="packageNum" htmlEscape="false" class="form-control"  maxlength="8" readonly="true" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">规格：</label>
                        <div class="col-sm-8">
                            <form:input path="packageSpec" id="packageSpec" htmlEscape="false" class="form-control"  maxlength="20" readonly="true"/>
                        </div>
                    </div>
                    <div class="form-group" id="packageUnits_add">
                        <label class="col-sm-4 control-label">单位：</label>
                        <div class="col-sm-8">
                            <form:select path="packageUnit" id="packageUnits" class="form-control"  disabled="true">
                                <form:option value="" label="" />
                                <form:options items="${fns:getDictList('SPEC_UNIT')}" itemLabel="label" itemValue="value" htmlEscape="false" />
                            </form:select>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-sm-4 control-label">最小规格：</label>
                        <div class="col-sm-8">
                            <form:input path="expProvideApplication.minSpec" id="minSpec" htmlEscape="false" class="form-control"  maxlength="20" readonly="true"/>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-sm-4 control-label">最小单位：</label>
                        <div class="col-sm-8">
                            <form:select path="expProvideApplication.minUnits" id="minUnits" class="form-control" disabled="true">
                                <form:option value="" label="" />
                                <form:options items="${fns:getDictList('SPEC_UNIT')}" itemLabel="label" itemValue="value" htmlEscape="false" />
                            </form:select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">厂家：</label>
                        <div class="col-sm-8">
                            <%--<form:input path="supplierId" htmlEscape="false" class="form-control"  maxlength="64" readonly="true"/>--%>
                            <form:select path="supplierId" id="supplierId" class="form-control" disabled="true">
                                <form:option value="" label="" />
                                <form:options items="${his_fns:getExpSupplierList()}" itemLabel="supplierName" itemValue="id" htmlEscape="false" />
                            </form:select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">进价：</label>
                        <div class="col-sm-8">
                            <form:input path="expPrice.tradePrice" id="purchasePrice" htmlEscape="false" class="form-control" data-parsley-required="true" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">批号：</label>
                        <div class="col-sm-8">
                            <form:input path="expStock.batchNo" id="batchNo" htmlEscape="false" class="form-control"  maxlength="64" data-parsley-required="true" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">有效期：</label>
                        <div class="col-sm-8">
                            <input id="expireDate" name="expStock.expireDate" htmlEscape="false" class="form-control Wdate" data-parsley-required="true" readonly
                                   onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">灭菌方式：</label>
                        <div class="col-sm-8">
                            <form:select path="expStock.sterilizationMethod" id="sterilizationMethod" htmlEscape="false" class="form-control" data-parsley-required="true">
                                <form:option value="" label=""/>
                                <form:options items="${fns:getDictList('STERILIZATION_METHOD')}" itemLabel="label" itemValue="value" htmlEscape="false" />
                            </form:select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">灭菌批号：</label>
                        <div class="col-sm-8">
                            <form:input path="expStock.sterilizationBatchNo" id="sterilizationBatchNo" htmlEscape="false" class="form-control"  maxlength="64" data-parsley-required="true" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">灭菌日期：</label>
                        <div class="col-sm-8">
                            <input id="sterilizationDate" name="expStock.sterilizationDate" htmlEscape="false" class="form-control Wdate" data-parsley-required="true" readonly
                                   onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">采购数量：</label>
                        <div class="col-sm-8">
                            <form:input path="expProvideApplication.auditQuantity" id="buyQuantity" htmlEscape="false" class="form-control"  maxlength="8" />
                        </div>
                    </div>
                </form:form>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <input id="btnSubmit" class="btn btn-primary" type="submit" value="确 定" onclick="addrows()">&nbsp;
                        <input id="btnCancel" class="btn" type="button" value="关 闭" onclick="closeRight()"></div>
                </div>
            </div>
        </section>
    </div>
</div>

<script>
    /*消耗品自动补全*/
    autoCompleteExpPrice("expName");
    function autoCompleteExpPrice(inputId){
        var url="${ctx}/csm/expInfo/autoCompletePrice";
        autoComplete(inputId,url,
                function(data){
                    var rows = [];
                    for(var i=0; i<data.length; i++){
                        rows[rows.length] = {
                            data:data[i],
                            value:data[i].id,
                            result:data[i].expDict.expName
                        };
                    }
                    return rows;
                },function(data, i, max){
                    if($.trim(data.expDict.expModel) != '' && data.expDict.expModel != null && typeof(data.expDict.expModel) != 'undefined'){
                        return data.expDict.expName+"|"+"|"+data.packageSpec + "/" + data.packageUnitsName + "|" + data.expDict.expModel + '|' + data.expSupplierCata.supplierJc;
                    }else{
                        return data.expDict.expName+"|"+"|"+data.packageSpec + "/" + data.packageUnitsName + "|"+data.expSupplierCata.supplierName;
                    }
                },function(event,data,formatted){
                    $('#expInfoId').attr('value',data.id);
                    $('#expCode').attr('value',data.expDict.expCode);
                    $('#expName').attr('value',data.expDict.expName);
                    $('#packageNum').attr('value',data.packageNum);
                    $('#packageSpec').attr('value',data.packageSpec);
                    $("#packageUnits option[value='"+data.packageUnit+"']").attr("selected", true);
                    $('#packageUnits').change();
                    $('#minSpec').attr('value',data.minSpec);
                    $("#minUnits option[value='"+data.minUnits+"']").attr("selected", true);
                    $('#minUnits').change();
                    $("#supplierId option[value='"+ data.expSupplierCata.id +"']").attr('selected',true);
                    $('#supplierId').change();
                })
    }
    function closeRight(){
        $("#stcokRightDiv").attr("style","display: none");
        $("#stcokLeftDiv").attr("class","main-content col-lg-12");
        $("#stcokLeftDiv").show('normal');
    }
</script>